package com.yict.common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SelectUtil {

	public final static String SELECT_PREFIX = "<select";

	public final static String SELECT_SUFFIX = "</select>";

	public final static String OPTION_PREFIX = "<option";

	public final static String OPTION_SUFFIX = "</option>";

	public final static String NAME = "name";

	public final static String ID = "id";

	public final static String VALUE = "value";

	public final static String EQUAL = "=";

	public final static String GT = ">";

	public final static String LT = "<";

	public final static String SPACE = " ";

	public final static String SPACE_STR = "\" \"";

	public final static String SELECT_ONE = "Select One";

	/**
	 * 
	 * @param list
	 * @param name
	 *            select属性name的值
	 * @param id
	 *            select属性id的值
	 * @return
	 */
	public static String list2Select(List<Map<String, Object>> list,
			String name, String id) {
		StringBuffer select = new StringBuffer();
		select.append(SELECT_PREFIX + SPACE + NAME + EQUAL + name + SPACE + ID
				+ EQUAL + id + GT);
		select.append(OPTION_PREFIX + SPACE + VALUE + EQUAL + SPACE_STR + GT
				+ SELECT_ONE + OPTION_SUFFIX);
		if (list != null && list.size() > 0) {
			for (Map<String, Object> map : list) {
				String itemKey = map.get("item_key").toString();
				String itemValue = map.get("item_value").toString();
				select.append(OPTION_PREFIX + SPACE + VALUE + EQUAL + itemKey
						+ GT + itemValue + OPTION_SUFFIX);
			}
		}
		select.append(SELECT_SUFFIX);
		return select.toString();
	}

	public static HashMap<String, String> toSearchHashMap(Map map) {
		HashMap<String, String> queryMap = new HashMap<String, String>();
		if (map != null) {
			Iterator iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				String key = (String) entry.getKey();
				String val = "";
				if (entry.getValue() != null) {
					val = (String) entry.getValue();
					if (key.indexOf("Id") != -1) {
						val = val.trim();
					} else {
						val = "%" + val.trim() + "%";
					}
					System.out.println("key:" + key);
					System.out.println("val:" + val);
					queryMap.put(key, val);
				}
			}
		}
		return queryMap;
	}

	public static Map<String, Object> toSearchMap(Map map) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (map != null) {
			Set<Map.Entry<String, Object>> entrys = map.entrySet();
			for (Map.Entry<String, Object> entry : entrys) {
				String key = entry.getKey();
				Object value = entry.getValue();
				if (value != null&&!"".equals(value)) {
					if (key.indexOf("Array") != -1) {
						String str[] = value.toString().split(",");
						queryMap.put(key, str);
					} else if (key.indexOf("List") != -1) {
						String str[] = value.toString().split(",");
						Long[] lo = new Long[str.length];
						for (int i = 0; i < str.length; i++) {
							lo[i] = ObjectUtil.s2l(str[i]);
						}
						queryMap.put(key, lo);
					} else if (key.indexOf("Id") != -1) {
						try {
							queryMap.put(key, Long.valueOf(value.toString()));
						} catch (NumberFormatException e) {
//							e.printStackTrace();
							queryMap.put(key, 0L);
						}
//						queryMap.put(key, Long.valueOf(value.toString()));
					} else if (key.indexOf("Date") != -1) {
						if(value.getClass() == String.class) {
							queryMap.put(key, value);
						}
						if(value.getClass() == Date.class) {
							queryMap.put(key, DateUtil.DateToString((Date)value, 1));
						}
					} else if (key.indexOf("Code") != -1) {
						queryMap.put(key, value);
					} else {
						queryMap.put(key, "%" + value.toString().toUpperCase() + "%");
					}
				}
			}
		}
		return queryMap;
	}
}
